package cheqex.update;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

import cheqex.update.controllers.*;

public class EditCollectionCashView extends JFrame implements ActionListener{
	
	private JTextField orNumber;
	private JTextField amount;
	private JTextField particular;
	private JPanel jp;
	private JPanel jl;
	private JButton next;
	private JButton save;
	private String numKey;
	private CollectionView collectionView;
	
	public JPanel getCollectionView() {
		return collectionView;
	}

	public void setCollectionView(CollectionView collectionView) {
		this.collectionView = collectionView;
	}

	
	public EditCollectionCashView(CollectionView collectionView){
		this.collectionView = collectionView;
		initialize(collectionView);
		
	}
	
	//closes login frame
	private void closeframe(){
		this.setVisible(false);
	}
	
	private void changePanel(){
		this.jl.setVisible(false);
		this.remove(jl);
		this.amount.setText(displayAmount());
		this.particular.setText(displayParticular());
		this.jp.setVisible(true);
		this.add(jp);
		this.setSize(250,180);
		
	}
	
	private String displayAmount(){
		try{
			if(!numKey.isEmpty()){
				int amt = CollectionController.getAmtCash(numKey);
				return Integer.toString(amt);
			}
			else return null;
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	private String displayParticular(){
		try{
			return CollectionController.getParticularCash(numKey);
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public void initialize(CollectionView collectionview){
		
		this.orNumber = new JTextField(20);
		orNumber.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					getNext();
				}
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		this.amount = new JTextField(20);
		amount.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					editData();
				}
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});

		this.particular = new JTextField(20);
		particular.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					editData();
				}
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		this.next = new JButton("Next");
		next.setMnemonic(KeyEvent.VK_D);
		next.setActionCommand("next");
		next.setEnabled(true);
		next.addActionListener(this);
		
		this.save = new JButton("Save");
		save.setMnemonic(KeyEvent.VK_D);
		save.setActionCommand("save");
		save.setEnabled(true);
		save.addActionListener(this);	
		
		JLabel orNo = new JLabel("OR Number:");
			orNo.setLabelFor(orNumber);
		JLabel amt = new JLabel("Amount:");
			amt.setLabelFor(amount);
		JLabel pr = new JLabel("Particular:");
			pr.setLabelFor(particular);
		
		jl = new JPanel(new FlowLayout(FlowLayout.TRAILING));
		jl.add(orNo);
		jl.add(orNumber);
		jl.add(next);
		jl.setVisible(true);	
			
		jp = new JPanel(new FlowLayout(FlowLayout.LEADING));
		jp.add(amt);
		jp.add(amount);
		jp.add(pr);
		jp.add(particular);
		jp.add(save);
		
		this.add(jl);
		this.pack();
		this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		this.setTitle("Editing");
	}
	

	private void editData(){
		int amt = -9999;
			String part = particular.getText();
			boolean er = true;
			
			int amtLength = this.amount.getText().length();
			if(amtLength!=0){
				if(amtLength>15){
					er = false;
					JOptionPane.showMessageDialog(null,"OR number is too long.", "Error", JOptionPane.WARNING_MESSAGE);
				}
				else
				if(!CollectionController.isNumeric(this.amount.getText())){
					er = false;
					JOptionPane.showMessageDialog(null,"Amount should only consists of digits.", "Error", JOptionPane.WARNING_MESSAGE);
				}else
				{
					amt = Integer.parseInt(this.amount.getText());
					if(amt==0){
						er = false;
						JOptionPane.showMessageDialog(null, "The amount should not be equal to zero.", "Error", JOptionPane.WARNING_MESSAGE);
					}
				}
			}
			
			if(er){
				int id = CollectionController.validateDataToBeAdded(numKey, amt, part);
				switch(id){
				case 1:	JOptionPane.showMessageDialog(null, 
						"No OR number entered.", "Error", JOptionPane.WARNING_MESSAGE);	
						break;		
				case 2:	JOptionPane.showMessageDialog(null, 
						"No OR number and amount entered.", "Error", JOptionPane.WARNING_MESSAGE);	
						break;
				case 3:	JOptionPane.showMessageDialog(null, 
						"No OR number, amount, and particular entered.", "Error", JOptionPane.WARNING_MESSAGE);	
						break;
				case 4:	JOptionPane.showMessageDialog(null, 
						"No OR number and particular entered.", "Error", JOptionPane.WARNING_MESSAGE);	
						break;
				case 5:	JOptionPane.showMessageDialog(null, 
						"No amount and particular entered.", "Error", JOptionPane.WARNING_MESSAGE);	
						break;
				case 6:	JOptionPane.showMessageDialog(null, 
						"No amount entered.", "Error", JOptionPane.WARNING_MESSAGE);	
						break;
				case 7:	JOptionPane.showMessageDialog(null, 
						"No particular entered.", "Error", JOptionPane.WARNING_MESSAGE);	
						break;		
				case 8:	try{		
						if(CollectionController.editCollectionCash(numKey, amt, part, DateController.getDate()))
							if(ActivitiesController.addActivity(DateController.getToday(), "Data deleted in collection cash table.")){
								JOptionPane.showMessageDialog(null,"Data Edited","Successful",JOptionPane.DEFAULT_OPTION);
								amount.setText(null);
								particular.setText(null);
								this.collectionView.populateTable();
							}else JOptionPane.showMessageDialog(null,"An error happened.","Error",JOptionPane.DEFAULT_OPTION);
							closeframe();
						} 
						catch (Exception a){a.printStackTrace();}
						break;
				}//end of switch			
			}
	}
	
	private void getNext(){
		boolean err = true;
		if(this.orNumber.getText().length()!=0){
			if(!CollectionController.isNumeric(this.orNumber.getText())){
				err = false;
				JOptionPane.showMessageDialog(null,"OR number should only consists of digits.", "Error", JOptionPane.WARNING_MESSAGE);
			}
		}
		else
		{
			err = false;
			JOptionPane.showMessageDialog(null,"Enter OR number.", "Error", JOptionPane.WARNING_MESSAGE);
		}
		
		if(err){
			try{
				if(CollectionController.findOR(orNumber.getText(), DateController.getDate())){
					numKey = orNumber.getText();
					orNumber.setText(null);
					changePanel();
				}else JOptionPane.showMessageDialog(null,"OR number does not exist.", "Error", JOptionPane.WARNING_MESSAGE);
			}catch (Exception a) { a.printStackTrace(); }
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if ("save".equals(e.getActionCommand())) {
			editData();
		}//end save
		
		if ("next".equals(e.getActionCommand())) {
			getNext();
		}//end next
	
		
	}
	
}
